package com.block.chlunion.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.block.chlunion.enums.UnionActionEnum;
import com.block.common.exception.NotifyExceptionService;
import com.block.common.service.LogFactory;
import com.block.db.entity.ChannelInfo;
import com.block.db.entity.ChannleUnionAddup;
import com.block.db.mapper.ChannleUnionAddupMapper;
import com.block.chlunion.service.IChannleUnionAddupService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 * 联登统计 服务实现类
 * </p>
 *
 * @author 
 * @since 2023-06-08
 */
@Service
public class ChannleUnionAddupServiceImpl extends ServiceImpl<ChannleUnionAddupMapper, ChannleUnionAddup> implements IChannleUnionAddupService {

    private static Logger log = LogFactory.getUnionLog();
    @Autowired
    NotifyExceptionService notifyExceptionService;

    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
    public void saveOrUpdateDataAddupInfo(String dateInfo, UnionActionEnum dataActionEnum, ChannelInfo channelInfo){

        try {
            ChannleUnionAddup channleUnionAddup = this.baseMapper.getEntityByDateInfoAndChannelCodeLock(dateInfo, channelInfo.getChannelCode());
            if (ObjectUtil.isEmpty(channleUnionAddup)) {
                try {
                    channleUnionAddup = this.saveNewEntity(dateInfo, channelInfo);
                    if (ObjectUtil.isEmpty(channleUnionAddup)) {
                        log.info("【渠道联登统计】新增统计信息为空【dateInfo】{}【configId】{}", dateInfo, channelInfo.getChannelCode());
                        return;
                    }
                } catch (Exception exception) {
                    log.error("【渠道联登统计】新增统计信息异常", exception);
                    String content = StrUtil.format("【dateInfo】{}【configId】{}", dateInfo, channelInfo.getChannelCode());
                    notifyExceptionService.notifyNoticeInfo("UNIONLOGIN", "新增联登统计信息异常", content, exception);
                }
            }

            ChannleUnionAddup updateEntity = new ChannleUnionAddup();
            updateEntity.setId(channleUnionAddup.getId());
            updateEntity.setUpdateTime(LocalDateTime.now());
            /**
             * 根据操作类型更新数据信息
             */
            if (StrUtil.equals(dataActionEnum.getValue(), UnionActionEnum.CHECK.getValue())) {
                updateEntity.setCheckNumber(1);
            } else if (StrUtil.equals(dataActionEnum.getValue(), UnionActionEnum.REGISTER.getValue())) {
                updateEntity.setLoginNumber(1);
            } else if (StrUtil.equals(dataActionEnum.getValue(), UnionActionEnum.SUCCESS.getValue())) {
                updateEntity.setLoginNumber(1);
                updateEntity.setSuccessNumber(1);
            }

            int updateRes = this.baseMapper.updateEntityById(updateEntity);
            if (updateRes > 0) {
                return;
            }
        } catch (Exception exception) {
            log.error("【渠道联登统计】联登统计异常", exception);
            String content = StrUtil.format("【dateInfo】{}【channelCode】{}", dateInfo, channelInfo.getChannelCode());
            notifyExceptionService.notifyNoticeInfo("UNIONLOGIN", "联登统计异常", content, exception);
        }
    }

    private ChannleUnionAddup saveNewEntity(String dateInfo, ChannelInfo channelInfo) {
        ChannleUnionAddup newEntity = new ChannleUnionAddup();
        newEntity.setDateInfo(dateInfo);
        newEntity.setChannelCode(channelInfo.getChannelCode());
        newEntity.setChannelName(channelInfo.getChannelName());

        boolean save = this.save(newEntity);
        if (!save) {
            return null;
        }
        return this.baseMapper.getEntityByDateInfoAndChannelCodeLock(dateInfo, channelInfo.getChannelCode());
    }

}
